package com.snow.useviewdemo.function2.other

import android.os.Bundle
import com.snow.useviewdemo.base.BaseKtActivity
import com.snow.useviewdemo.databinding.ActivityPreferencesDatastoreTestBinding
import com.snow.useviewdemo.util.LogUtils
import com.snow.useviewdemo.util.PreferencesDataStoreUtils
import kotlinx.coroutines.*

/**
 * Created by zhyt
 * Date: 2022/9/26
 * Describe:
 */
class PreferencesDatastoreActivity : BaseKtActivity<ActivityPreferencesDatastoreTestBinding>() {
    //    override fun getLayoutId(): Int {
//        return R.layout.activity_preferences_datastore_test
//    }
    override fun setViewData(savedInstanceState: Bundle?) {
        val test_key01 = "test_preferences_data_key"
        CoroutineScope(Dispatchers.IO).launch {
            PreferencesDataStoreUtils.getInstance()
                .putString(test_key01, "I is PreferencesDatastore mssds")
        }
        CoroutineScope(Dispatchers.Main).launch {  //切换主线程准备进行跟新UI操作
            var string = withContext(Dispatchers.IO) { //切换IO线程进行获取数据
                PreferencesDataStoreUtils.getInstance().getString(test_key01) //获取数据
            }
            //自动切换为主线程，更新UI
            binding.textShow.text = string
        }
    }

    override fun getViewBinding() = ActivityPreferencesDatastoreTestBinding.inflate(layoutInflater)
}